//
//  JMHomeViewController.m
//  仿生物无忧
//
//  Created by 彭继宗 on 15/12/5.
//  Copyright © 2015年 彭继宗. All rights reserved.
//

#import "JMHomeViewController.h"

#import "JMtopSeleteView.h"
#import "JMFocusVideos.h"
#import "JMpicReusableView.h"
#import "JMlistVideosCell.h"

@interface JMHomeViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>

//顶部选择view
@property (nonatomic, strong) JMtopSeleteView *topSeletedView;


//视频view
@property (nonatomic, strong) UICollectionView *collectionView;

//视频列表数组
@property (nonatomic, strong) NSArray *listVideoArray;
//滚动视频数组
@property (nonatomic, strong) NSArray *focusVideoArray;

//左边登陆按钮
@property (nonatomic, strong) UIButton *loginButton;
//右边扫描二维码按钮
@property (nonatomic, strong) UIButton *scanButton;

//布局
@property (nonatomic, strong) UICollectionViewFlowLayout *flowLayout;

@end

@implementation JMHomeViewController

static NSString * const reuseIdentifier = @"Cell";

static NSString * const headerViewIdentifier = @"headerView";



- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self prepareUI];
    
    [self sendNetWork];
}


#pragma mark- 准备UI
- (void)prepareUI
{
    
    //注册cell
    [self.collectionView registerNib:[UINib nibWithNibName:@"JMlistVideosCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:reuseIdentifier];
    
    [self.collectionView registerNib:[UINib nibWithNibName:@"JMpicReusableView" bundle:[NSBundle mainBundle]] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerViewIdentifier];
    
    //设置delegate和datasourse
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    
    
    //添加子控件
    [self.view addSubview:self.topSeletedView];
    [self.view addSubview:self.collectionView];
    
    //登陆按钮
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"user"] landscapeImagePhone:nil style:UIBarButtonItemStylePlain target:self action:@selector(loginButtonClick)];
    //扫描按钮
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"RichScan"] landscapeImagePhone:nil style:UIBarButtonItemStylePlain target:self action:@selector(scanButtonClick)];
    
    
    /**
     *  添加约束
     */
    [self.topSeletedView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.width.equalTo(self.view);
        make.height.equalTo(@44);
        make.top.equalTo(self.view.mas_top).with.offset(64);
    }];
    
    //滚动
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.width.equalTo(self.view);
        make.height.equalTo (@(self.view.bounds.size.height-64-44-49));
        make.top.equalTo(self.topSeletedView.mas_bottom);
    }];
    
    /**
     *  设置collectionView布局
     *
     */
    self.flowLayout.itemSize = CGSizeMake(self.view.bounds.size.width /2 -2 , 150);
    //取消间距
    self.flowLayout.minimumLineSpacing = 2 ;
    self.flowLayout.minimumInteritemSpacing = 4;
    //设置headView高度
    self.flowLayout.headerReferenceSize = CGSizeMake(self.view.bounds.size.width, 190);
}

#pragma mark- 按钮点击事件
- (void)loginButtonClick
{
    [[JMNetworkTools shareNetworkTools] GET:@"http://www.swwy.com/app/android/newsApi.php?authKey=0ac02eaf32cc78a7e10abe4d40bf25ec&cid=47&device=4&page=1&pageSize=10" parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        NSLog(@"%@",responseObject);
    } failure:nil];
}

- (void)scanButtonClick
{
    
}




#pragma mark- <UICollectionViewDataSource>

//返回多少组
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    
    return 1;
}

//返回多少个cell
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (self.listVideoArray.count == 0)
    {
        return 10;
    }
    
    return self.listVideoArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    JMlistVideosCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    
    
    cell.backgroundColor = [UIColor colorWithWhite:0.98 alpha:1];
    
    if (self.listVideoArray.count != 0)
    {
        cell.video = self.listVideoArray[indexPath.row];
    }
    
    
    return cell;
}

#pragma mark- 发送网络请求
- (void)sendNetWork
{
    [[JMNetworkTools shareNetworkTools] loadTopViewPicWithFinish:^(NSDictionary *result, NSError *error) {
        
        self.listVideoArray = [JMListVideos listVideos:result[@"data"][@"list"]];
    
        [self.collectionView reloadData];
                
    }];
    
    
}


#pragma mark- UICollectionReusableView
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    //定义重用view
    JMpicReusableView *reusableview = nil;
    
    //如果是头部view
    if (kind == UICollectionElementKindSectionHeader){
        
        //重用注册cell
        JMpicReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerViewIdentifier forIndexPath:indexPath];
        
        
        [[JMNetworkTools shareNetworkTools] loadTopViewPicWithFinish:^(NSDictionary *result, NSError *error) {
            
            _focusVideoArray = [JMFocusVideos listVideos:result[@"data"][@"focus"]];
            
            //轮播器
            headerView.picArray = [self.focusVideoArray valueForKeyPath:@"picUrl"];
        }];
        
        reusableview = headerView;
        
    }
    
    return reusableview;
    
    
}


#pragma mark- 懒加载
- (JMtopSeleteView *)topSeletedView
{
    if (_topSeletedView == nil)
    {
        _topSeletedView = [JMtopSeleteView topViewWithNum:4 andTitle:@[@"医学",@"生命科学",@"无忧学院",@"分类"] titleColor:basicColor];
        
    }
    return _topSeletedView;
}

- (UICollectionView *)collectionView
{
    if (_collectionView == nil)
    {
        self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
        
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.flowLayout];
        
        _collectionView.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1];
    }
    return _collectionView;
}


- (NSArray *)listVideoArray
{
    if (_listVideoArray == nil)
    {
        _listVideoArray = [[NSArray alloc] init];
        
    }
    return _listVideoArray;
}

- (NSArray *)focusVideoArray
{
    if (_focusVideoArray == nil)
    {
        _focusVideoArray = [[NSArray alloc] init];
    }
    return _focusVideoArray;
}

@end
